package com.suian.springbootmysql.service.impl;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.suian.springbootmysql.dao.LoginDao;
import com.suian.springbootmysql.dao.UserDao;
import com.suian.springbootmysql.pojo.ResultVo;
import com.suian.springbootmysql.pojo.TbUser;
import com.suian.springbootmysql.service.LoginService;
import com.suian.springbootmysql.utils.AliCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;


@Service

public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserDao userDao;

    //手机号是否存在 不存在就成功
    @Override
    public ResultVo sendCode(String phone) throws  Exception{
        boolean isPhone = Validator.isMobile(phone);
        if (isPhone){
          int count=  loginDao.getUserCouneByphone(phone);
          if (count>0){
              AliCodeUtils aliCodeUtils=new AliCodeUtils();
              aliCodeUtils.sendLoginCode(phone,"123455");
              redisTemplate.opsForValue().set(phone,"123455");
              redisTemplate.expire(phone,5, TimeUnit.MINUTES);
              return  new ResultVo(200,"成功",null);

          }else {
              return  new ResultVo(303,"手机号不存在",null);
          }
        }else {
            return  new ResultVo(303,"格式错误",null);
        }
    }


    //登录 手机号+验证码登录
    // phone 登录手机号   code 用户输入的验证码
    public ResultVo userLogin(String phone, String code) {
        //1,判断手机号验证码是否填写
        boolean isHavephone = StrUtil.isEmpty(phone);
        boolean isHavecode = StringUtils.isEmpty(code);
        if (!isHavephone){
            if (!isHavecode){
                //2，判断用户是否存在
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getPhone,phone);
                TbUser tbUser=userDao.selectOne(lambdaQueryWrapper);
//                int count=  loginDao.getUserCouneByphone(phone);
                if (tbUser!=null){
                    //3,验证码是否正确
                    String redisCode=redisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)){
                        if (redisCode.equals(code)){
                            //4,返回个人信息
                            return new ResultVo(200,"success",tbUser);
                        }else {
                            return new ResultVo(303,"验证码错误",null);
                        }
                    }else {
                        return new ResultVo(303,"验证码超时",null);
                    }

                }else {
                    return  new ResultVo(303,"不存在或未激活",null);
                }


            }else {
                return  new ResultVo(303,"验证码必填",null);
            }
        }else {
            return  new ResultVo(303,"手机号必填",null);
        }

    }
}
